package com.example.btchat;

import org.json.JSONException;
import org.json.JSONObject;

public class Message
{
	public static Message fromJSON(JSONObject obj) throws JSONException
	{
		String message, source;
		message = obj.getString("message");
		source = obj.getString("source");
		return new Message(source, message);
	}
	
	public static Message fromString(String data) throws JSONException
	{
		JSONObject obj = new JSONObject(data);
		
		return Message.fromJSON(obj);
	}
	
	private String message;
	private String source;
	
	public Message(String source, String message)
	{
		this.source = source;
		this.message = message;
	}
	
	public String getMessage()
	{
		return message;
	}
	
	public String getSource()
	{
		return source;
	}
	
	public JSONObject toJSON() throws JSONException
	{
		JSONObject obj = new JSONObject();
		obj.put("message", message);
		obj.put("source", source);
		return obj;
	}
	
	public String toString()
	{
		try
		{
			return this.toJSON().toString();
		}
		catch(JSONException e)
		{
			return "";
		}
	}
}
